package com.yljk.yljkproject.utils;

import android.app.Application;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import com.heytap.msp.push.HeytapPushManager;
import com.heytap.msp.push.callback.ICallBackResultService;
import com.huawei.hms.aaid.HmsInstanceId;
import com.huawei.hms.common.ApiException;
import com.hyphenate.chat.EMClient;
import com.yilijk.base.utils.ALog;
import com.yilijk.base.utils.DevicesUtils;
import com.yilijk.base.utils.SharedPreferencesUtils;

import org.android.agoo.huawei.HuaWeiRegister;
//import org.android.agoo.mezu.MeizuRegister;
import org.android.agoo.vivo.VivoRegister;
import org.android.agoo.xiaomi.MiPushRegistar;

/**
 * Created by SHICHENG
 * <p>
 * Time on 2021/11/3
 *
 * PushUtils.getPushUtils().initRomDevice(this);
 *
 */
public class PushUtils {

    private static PushUtils pushUtils;
    public synchronized static PushUtils getPushUtils() {
        if (pushUtils == null){
            pushUtils = new PushUtils();
        }
        return pushUtils;
    }

    /**
     * 初始化设备的厂商推送信息
     * @param application
     */
    public void initRomDevice(Application application){
        //判断手机
        if (RomUtil.isEmui()) {
            HMSUtils.initHuaWeiPush(application);
        } else if (RomUtil.isMiui()) {
            XiaoMiUtils.initXiaoMiPush(application);
        } else if (RomUtil.isVivo()) {
            initVivoPush(application);
        } else if (RomUtil.isOppo()) {
            initOppoPush(application);
        } else if (RomUtil.isFlyme()) {
            initFlymePush(application);
        } else if (RomUtil.is360()) {
            init360Push();
        }
    }

    /**
     * HMS
     */
    public static class HMSUtils{
        /**
         * 华为厂商推送
         * @param application
         */
        private static void initHuaWeiPush(Application application) {
            ALog.i("HuaWei", "initPush: ");
            HuaWeiRegister.register(application); // 华为
        }
        /**
         * 获取HMS PUSH TOKEN
         */
        public static void getHMSPushToken(Application application){
            // 创建一个新线程
            new Thread() {
                @Override
                public void run() {
                    try {
                        // 从agconnect-service.json文件中读取appId
                        String appId = "103996803";

                        // 输入token标识"HCM"
                        String tokenScope = "HCM";
                        String token = HmsInstanceId.getInstance(application).getToken(appId, tokenScope);
//                        Log.i(TAG, "get token: " + token);
                        ALog.e("HuaWeiRegister","token ======>"+token);
                        // 判断token是否为空
                        if(!TextUtils.isEmpty(token)) {
//                            sendRegTokenToServer(token);
                            SharedPreferencesUtils.getInstance().saveUment("HMSToken",token);
                            new Handler(Looper.getMainLooper()).post(new Runnable() {
                                @Override
                                public void run() {
                                    // 上传 token 方法，token 是通过广播接收器接收到的
                                    EMClient.getInstance().sendHMSPushTokenToServer(appId, token);
                                }
                            });
                            //IQAAAACy0gx4AAAWAYuOKltHpAgTF9zcFZbbDnkNLxTSJVnEOlzB34PqrzvQG_alST4xYz1-idNUrE3uISeIs2SAO6FPJJLytqdvXBaczAoSau7ntA
                        }
                    } catch (ApiException e) {
//                        Log.e(TAG, "get token failed, " + e);
                    }
                }
            }.start();
        }

//        private void sendRegTokenToServer(String token) {
//            Log.i(TAG, "sending token to server. token:" + token);
//        }

    }

    /**
     * 360厂商推送
     */
    private void init360Push() {
        ALog.i("360", "initPush: ");
    }

    /**
     * 魅族厂商推送
     * @param application
     */
    private void initFlymePush(Application application) {
        ALog.i("魅族", "initPush: ");
//        MeizuRegister.register(application, "140292", "688b6b8981c74bb9983e8f6feb86b27f");
    }

    /**
     * OPPO厂商推送
     * @param application
     */
    private void initOppoPush(Application application) {
        ALog.i("Oppo", "initPush: ");
//        OppoRegister.register(application, "5675b5657ebf4105ab1a192bdf5f85bc", "9ab25a13df854a5bbf4a4ab1e7b8c815");
//        OppoRegister.register();
        HeytapPushManager.init(application, DevicesUtils.isEnvironment());
        if (HeytapPushManager.isSupportPush()){
            HeytapPushManager.register(application, "5675b5657ebf4105ab1a192bdf5f85bc", "9ab25a13df854a5bbf4a4ab1e7b8c815", new ICallBackResultService() {
                @Override
                //注册的结果,如果注册成功,registerID就是客户端的唯一身份标识
                public void onRegister(int responseCode, String registerID) {
                    ALog.e("registerID_OPPO",registerID);
                    //OPPO_CN_f5289153a82dc537c34d46917794e039
                }

                @Override
                //反注册的结果
                public void onUnRegister(int responseCode) {

                }

                @Override
                //获取设置推送时间的执行结果
                public void onSetPushTime(int responseCode, String pushTime) {

                }

                @Override
                //获取当前的push状态返回,根据返回码判断当前的push状态,返回码具体含义可以参考[错误码]
                public void onGetPushStatus(int responseCode, int status) {

                }

                @Override
                //获取当前通知栏状态，返回码具体含义可以参考[错误码]
                public void onGetNotificationStatus(int responseCode, int status) {

                }

            });
        }
    }

    /**
     * VIVO厂商推送
     * @param application
     */
    private void initVivoPush(Application application) {
        ALog.i("Vivo", "initPush: ");
        VivoRegister.register(application);
    }



    public static class XiaoMiUtils{

        /**
         * 小米厂商推送
         * @param application
         */
        private static void initXiaoMiPush(Application application) {
            ALog.i("XiaoMi", "initPush: ");
            MiPushRegistar.register(application, "2882303761519857784", "5321985739784");
        }
    }






}
